package org.example.server.pop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.enums.TaskEnum;
import org.example.po.*;
import org.example.utils.IpUtils;
import org.example.utils.MD5Util;
import org.example.utils.http.RedirectUtil;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ToPop {

    public static PopCorpClientManagerDto popLogin(String phone, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("regMobile", phone);
        map.put("password", password);
        String s = RedirectUtil.doGetParam("https://h-api.ybm100.com/api/hack/pop/login", map);
        ApiRPCResult apiRPCResult = JSON.parseObject(s, ApiRPCResult.class);
        PopCorpClientManagerDto popCorpClientManagerDto = JSON.parseObject(JSON.toJSONString(apiRPCResult.getData()), PopCorpClientManagerDto.class);
        System.out.println(JSON.toJSONString(popCorpClientManagerDto));
        return popCorpClientManagerDto;
    }

    /**
     * 商品全量上报
     */
    public static void product_full(String phone, String password) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(phone, password);
        String json = "{\n" +
                "    \"production_area\": \"\",\n" +
                "    \"near_effect\": \"2025-08-24\",\n" +
                "    \"code\": \"0\",\n" +
                "    \"medium_package_num\": \"1\",\n" +
                "    \"market_author\": \"\",\n" +
                "    \"monomer_price\": 34.9,\n" +
                "    \"new_pro_date\": \"2023-08-25\",\n" +
                "    \"erp_code\": \"H00061\",\n" +
                "    \"product_name\": \"奥氮平片\",\n" +
                "    \"product_unit\": \"盒\",\n" +
                "    \"spec\": \"5mg*20片\",\n" +
                "    \"manufacturer\": \"\",\n" +
                "    \"dosage_form\": \"片剂\",\n" +
                "    \"chain_price\": 34.9,\n" +
                "    \"far_effect\": \"2025-08-24\",\n" +
                "    \"approval_number\": \"H20150141\",\n" +
                "    \"term\": 24,\n" +
                "    \"common_name\": \"奥氮平片\",\n" +
                "    \"suggest_price\": \"9999\",\n" +
                "    \"available_qyt\": 60,\n" +
                "    \"oldest_pro_date\": \"2023-08-25\"\n" +
                "  }";
        PopErpSkuInfoDto popErpSkuInfoDto = JSON.parseObject(json, PopErpSkuInfoDto.class);
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", popCorpClientManagerDto.getOrgId());
        map.put("appId", popCorpClientManagerDto.getAppId());
        map.put("taskId", 10001);

        List<PopErpSkuInfoDto> popErpSkuInfoDtos = new ArrayList<>();

        popErpSkuInfoDtos.add(popErpSkuInfoDto);
        map.put("jsonData", JSON.toJSONString(popErpSkuInfoDtos));
        map.put("sign", MD5Util.encrypt(map, popCorpClientManagerDto.getAppSecret()));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(map));
        System.out.println(JSON.toJSONString(map));
        System.out.println(s);

    }
    /**
     * 商品更新上报
     */
    public static void product_gengxin(String phone, String password) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(phone, password);
        String json = "{\n" +
                "    \"production_area\": \"\",\n" +
                "    \"near_effect\": \"2025-08-24\",\n" +
                "    \"code\": \"0\",\n" +
                "    \"medium_package_num\": \"1\",\n" +
                "    \"market_author\": \"\",\n" +
                "    \"monomer_price\": 34.9,\n" +
                "    \"new_pro_date\": \"2023-08-25\",\n" +
                "    \"erp_code\": \"H00061\",\n" +
                "    \"product_name\": \"奥氮平片\",\n" +
                "    \"product_unit\": \"盒\",\n" +
                "    \"spec\": \"5mg*20片\",\n" +
                "    \"manufacturer\": \"\",\n" +
                "    \"dosage_form\": \"片剂\",\n" +
                "    \"chain_price\": 34.9,\n" +
                "    \"far_effect\": \"2025-08-24\",\n" +
                "    \"approval_number\": \"H20150141\",\n" +
                "    \"term\": 24,\n" +
                "    \"common_name\": \"奥氮平片\",\n" +
                "    \"suggest_price\": \"9999\",\n" +
                "    \"available_qyt\": 60,\n" +
                "    \"oldest_pro_date\": \"2023-08-25\"\n" +
                "  }";
        PopErpSkuInfoDto popErpSkuInfoDto = JSON.parseObject(json, PopErpSkuInfoDto.class);
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", popCorpClientManagerDto.getOrgId());
        map.put("appId", popCorpClientManagerDto.getAppId());
        map.put("taskId", 20001);

        List<PopErpSkuInfoDto> popErpSkuInfoDtos = new ArrayList<>();

        popErpSkuInfoDtos.add(popErpSkuInfoDto);
        map.put("jsonData", JSON.toJSONString(popErpSkuInfoDtos));
        map.put("sign", MD5Util.encrypt(map, popCorpClientManagerDto.getAppSecret()));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(map));
        System.out.println(JSON.toJSONString(map));
        System.out.println(s);

    }

    /**
     * 客户信息上报
     */
    public static void erpToPopCustomer(String userName, String passWord) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(userName, passWord);
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", popCorpClientManagerDto.getOrgId());
        map.put("appId", popCorpClientManagerDto.getAppId());
        map.put("taskId", 90001);
        List<PopErpClientInfoDto> popErpClientInfoDtos = new ArrayList<>();
        PopErpClientInfoDto popErpClientInfoDto = new PopErpClientInfoDto();
        popErpClientInfoDto.setOrgId(popCorpClientManagerDto.getOrgId());
        popErpClientInfoDto.setCustomerId("0000001");
        popErpClientInfoDto.setCustomerCode("000001");
        popErpClientInfoDto.setCustomerName("石家庄鑫顺堂大药房有限公司");
        //popErpClientInfoDtos.add(popErpClientInfoDto);
        popErpClientInfoDtos.add(popErpClientInfoDto);
        map.put("jsonData", JSON.toJSONString(popErpClientInfoDtos));
        map.put("sign", MD5Util.encrypt(map, popCorpClientManagerDto.getAppSecret()));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(map));
        org.example.po.Response response = JSON.parseObject(s, Response.class);
        String cust = "";
        for (PopErpClientInfoDto erpClientInfoDto : popErpClientInfoDtos) {
            cust = String.join(",", erpClientInfoDto.getCustomerName());
        }
        if (response.getCode() == 200 && response.getData().equals("true")) {
            System.out.println("客户上传成功--{}" + cust);
        } else {
            System.out.println("客户信息上报失败");
        }
    }

    /**
     * 订单回调
     */
    public static void erpCallPop(String orgId, String appId, String appSecret) {
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("appId", appId);
        List<CallOrder> orders = new ArrayList<>();
        CallOrder order = new CallOrder();
        order.setOrderNo("YBM20240618120838141881");
        order.setType(1);
        order.setResult("true");
        order.setOrgId(orgId);
        orders.add(order);
        map.put("jsonData", JSON.toJSONString(orders));
        map.put("sign", MD5Util.encrypt(map, appSecret));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/callbackForPullTask", JSON.toJSONString(map));
        System.out.println(s);
        System.out.println("请求参数" + JSON.toJSONString(map));
    }

    /**
     * 出库明细回传
     */
    public static void erpToPopOrderDetail(String userName, String passWord) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(userName, passWord);
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", popCorpClientManagerDto.getOrgId());
        map.put("appId", popCorpClientManagerDto.getAppId());
        map.put("taskId", 80001);
        List<SellerOrderDeliverDetailForErpDto> sellerOrderDeliverDetailForErpDtos = new ArrayList<>();
        SellerOrderDeliverDetailForErpDto popErpSkuInfoDto = new SellerOrderDeliverDetailForErpDto();
        popErpSkuInfoDto.setOrderNo("YBM20250313172032166162");
        popErpSkuInfoDto.setOrderDetailId(241173720L);
        popErpSkuInfoDto.setErpCode("HZKe226076");
        popErpSkuInfoDto.setProductName("骏隆 冬白梅片(骏隆)");
        popErpSkuInfoDto.setBatchCode("11111");
        popErpSkuInfoDto.setBatchNumDecimal(BigDecimal.valueOf(300.000));
        popErpSkuInfoDto.setBatchValidDate("2025-01-31");
        popErpSkuInfoDto.setProductionDate("2020-02-17");
        popErpSkuInfoDto.setBatchPrice(BigDecimal.valueOf(4.29));
        sellerOrderDeliverDetailForErpDtos.add(popErpSkuInfoDto);
        map.put("jsonData", JSON.toJSONString(sellerOrderDeliverDetailForErpDtos));
        map.put("sign", MD5Util.encrypt(map, popCorpClientManagerDto.getAppSecret()));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(map));
        System.out.println("请求参数-->>" + JSON.toJSONString(map));
        System.out.println(s);
    }

    /**
     * 商品价格
     */
    public static void erpToPopPrice(String userName, String passWord) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(userName, passWord);
        List<PopErpSkuPriceDto> popErpSkuPriceDtos = new ArrayList<>();
        PopErpSkuPriceDto popErpSkuPriceDto = new PopErpSkuPriceDto();
        popErpSkuPriceDto.setErpCode("1.01.E160002.50*4*60");
        popErpSkuPriceDto.setChainPrice(BigDecimal.valueOf(9999));
        popErpSkuPriceDto.setMonomerPrice(BigDecimal.valueOf(9999));
        popErpSkuPriceDto.setBasePrice(BigDecimal.valueOf(9999));
        popErpSkuPriceDtos.add(popErpSkuPriceDto);
        //构建pop订单查询参数
        //构建查询pop参数
        Map<String, Object> objectMap = buildPopTaskQueryParam(popCorpClientManagerDto.getOrgId(), popCorpClientManagerDto.getAppId(), popCorpClientManagerDto.getAppSecret(), TaskEnum.PRODUCT_PRICE.getTaskId(), popErpSkuPriceDtos);
        String body = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(objectMap));
        System.out.println(body);
    }

    /**
     * 商品价格
     */
    public static void erpToPopStock(String orgId, String appId, String appSecret) {
        List<PopErpSkuStockV2Dto> popErpSkuStockV2Dtos = new ArrayList<>();
        PopErpSkuStockV2Dto popErpSkuStockV2Dto = new PopErpSkuStockV2Dto();
        popErpSkuStockV2Dto.setErpCode("H00061");
        popErpSkuStockV2Dto.setAvailableQyt(10);
        popErpSkuStockV2Dto.setOldestProDate("2023-12-11");
        popErpSkuStockV2Dto.setNewProDate("2023-12-11");
        popErpSkuStockV2Dto.setNearEffect("2024-12-11");
        popErpSkuStockV2Dto.setFarEffect("2024-12-11");
        popErpSkuStockV2Dtos.add(popErpSkuStockV2Dto);
        //构建pop订单查询参数
        //构建查询pop参数
        Map<String, Object> objectMap = buildPopTaskQueryParam(orgId, appId, appSecret, TaskEnum.PRODUCT_STOCK.getTaskId(), popErpSkuStockV2Dtos);
        log.info("库存接口入参-->{}", JSON.toJSONString(objectMap));
       String body = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(objectMap));
       System.out.println(body);
    }

    //构建参数方法
    private static <T> Map<String, Object> buildPopTaskQueryParam(String orgId, String appId, String appSecret, Integer taskId, List<T> list) {


        TaskQueryParam taskQueryParam = new TaskQueryParam();
        taskQueryParam.setOrgId(orgId);
        taskQueryParam.setAppId(appId);
        if (taskId != null && taskId != 00000) {
            taskQueryParam.setTaskId(taskId);
        }
        if (taskId == 00000) {
            String ipAddress = "";
            InetAddress address = IpUtils.getLocalHostLANAddress();
            if (address != null) {
                ipAddress = address.getHostAddress();
            }
            taskQueryParam.setVersion("3.4");
            taskQueryParam.setJsonData(ipAddress);
        }
        if (!CollectionUtils.isEmpty(list)) {
            taskQueryParam.setJsonData(JSON.toJSONString(list));
        }
        Map<String, Object> popParamMap = JSON.parseObject(JSON.toJSONString(taskQueryParam)).getInnerMap();
        taskQueryParam.setSign(MD5Util.encrypt(popParamMap, appSecret));
        Map<String, Object> popParamMapNew = JSON.parseObject(JSON.toJSONString(taskQueryParam)).getInnerMap();
        return popParamMapNew;
    }

    /**
     * 发票回传
     */
    public static void erpToPopInvoice(String userName, String passWord) {
        PopCorpClientManagerDto popCorpClientManagerDto = popLogin(userName, passWord);
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", popCorpClientManagerDto.getOrgId());
        map.put("appId", popCorpClientManagerDto.getAppId());
        map.put("taskId", 130001);
        List<PopErpInovicesDto> orders = new ArrayList<>();
        PopErpInovicesDto inovicesDto = new PopErpInovicesDto();
        inovicesDto.setOrderNo("YBM20250224144519158984");
        inovicesDto.setOrderInvoiceUrl("https://dz-fp.cn/DzFP/FP?XYM=X1703564064BB89F,https://dz-fp.cn/DzFP/FP?XYM=X1421184064B1B02");
        orders.add(inovicesDto);
        map.put("jsonData", JSON.toJSONString(orders));
        map.put("sign", MD5Util.encrypt(map, popCorpClientManagerDto.getAppSecret()));

        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/pushDataInfo", JSON.toJSONString(map));
        System.out.println("请求参数-->>" + JSON.toJSONString(map));
        System.out.println(s);
    }


    /**
     * 订单回调
     */
    public static void erpCallPopCustomer(String orgId, String appId, String appSecret) {

        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("appId", appId);
        List<SellerCustomerCallBackDto> orders = new ArrayList<>();
        SellerCustomerCallBackDto order = new SellerCustomerCallBackDto();
        order.setNo("河北燊道医药销售有限公司");
        order.setId(1111313392L);
        order.setResult(true);
        order.setOrgId(orgId);
        orders.add(order);
        map.put("jsonData", JSON.toJSONString(orders));
        map.put("sign", MD5Util.encrypt(map, appSecret));
        String s = RedirectUtil.doPostByJson("https://h-api.ybm100.com/api/hackTask/callbackForCustomerPullTask", JSON.toJSONString(map));
        System.out.println(s);
        System.out.println("请求参数" + JSON.toJSONString(map));
    }
}
